﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SmallBusinessKit.DataAccess.Repositories;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.Tests.Repositories
{
    /// <summary>
    /// Unit Tests for XmlItemRepository Logic
    /// </summary>
    [TestClass]
    public class XmlItemRepositoryTests : UnitTestBase
    {
        #region Properties

        private XmlItemRepository _xmlRep;

        #endregion Properties

        #region Tests

        [TestInitialize]
        public override void Setup()
        {
            base.Setup();

            var items = XmlData.ParseItems();           // use data from file to save time...
            items.ElementAt(2).IsVisible = false;       // to test GetMany(i => i.IsVisible)
            items.ElementAt(4).IsVisible = false;       // to test GetMany(i => i.IsVisible)

            _xmlRep = new XmlItemRepository(items);
        }

        [TestMethod]
        public void XmlItemGetById()
        {
            // Act
            var result = _xmlRep.GetById("02");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Item));
            Assert.AreEqual(200.00m, result.Price);
        }

        [TestMethod]
        public void XmlItemGet()
        {
            // Act
            var result = _xmlRep.Get(i => i.Id == "02");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Item));
            Assert.AreEqual("02", result.Id);
        }

        [TestMethod]
        public void XmlItemGetMany()
        {
            // Act
            var results = _xmlRep.GetMany(i => i.IsVisible).ToList();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof (IEnumerable<Item>));
            Assert.AreEqual(8, results.Count());
        }

        [TestMethod]
        public void XmlItemGetAll()
        {
            // Act
            var results = _xmlRep.GetAll().ToList();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof(IEnumerable<Item>));
            Assert.AreEqual(10, results.Count());
        }
        
        #endregion
    }
}
